package classes.multithread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : DuJiabao
 * @Date : 2021/1/20 16:51
 * @Project : java_learning
 * @File : null.java
 * @Desc :
 */
public class SynchronizedTest2 {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 3; i++) {
            exec.execute(new SaleTickets2());
        }
        exec.shutdown();
    }
}

class SaleTickets2 implements Runnable {
    private static int tickets = 1000;

    @Override
    public void run() {
        while (true) {
            synchronized (SaleTickets2.class) {
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + " " + tickets--);
                } else {
                    break;
                }
            }
        }
    }
}

